<?php

if (!function_exists('getSmsValue')) {
    /**
     * 拼接短信模板内容
     * @param  array  $data [description]
     * @return string       [description]
     */
    function getSmsValue($data = array())
    {
        $result = array();
        foreach ($data as $key => $value) {
            $result[] = "#{$key}#={$value}";
        }
        return implode('&', $result);
    }
}

/**
 * Created by PhpStorm.
 * User: huyang
 * Date: 2017/6/9
 * Time: 下午2:58
 *
 * 发送短信验证码 - 聚合数据
 * @param  [string] $phone  [手机号]
 * @param int $type 短信类型，1=注册/修改密码,2=支付成功通知
 * @param array $data 模板内容数据
 * 
 */
if (!function_exists('sms_juhe')) {
    function sms_juhe($type, $phone, $data = array())
    {
        $httpInfo = array();
        // $tplId = 36723;  // 养悦源
        if($type == 1)//注册、修改密码 验证码
        {
            $tplId = 36723;
        }
        else if($type == 2)//支付成功通知模板
        {
            $tplId = 37509;
        }
        else
        {
            return false;//不存在的短信模板
        }
        // $value = urlencode("#code#={$number}&#app#={养悦源}");
        $value = urlencode(getSmsValue($data));
        $url = "http://v.juhe.cn/sms/send?mobile={$phone}&tpl_id={$tplId}&tpl_value={$value}&key=ce4626c664d26ea8269197e50fe05d8b";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        if ($response === false) {
//            echo "cURL Error: " . curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
        $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );

        curl_close($ch);
        return $response;
    }
}






?>